package com.example.demo.controller.admin;

import com.example.demo.entity.Label;
import com.example.demo.service.admin.LabelAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/admin/label")
@Controller
public class LabelAdminController {

    @Autowired
    private LabelAdminService labelAdminService;

    /**
     * 添加标签 返回添加标签界面
     * @param model
     * @return
     */
    @RequestMapping(value = "/editLabels",method = RequestMethod.GET)
    public String toLabel(Model model) {
        model.addAttribute("label",labelAdminService.selectAll());
        return "/admin_editLabels";
    }

    /**
     *查询标签 返回查询标签界面
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "/optionLabel",method = RequestMethod.GET)
    public String optionLabel(Integer id,Model model) {
        model.addAttribute("label",labelAdminService.selectLabelById(id));
        return "/admin_optionLabel";
    }

    /**
     * 查询所有标签
     * @author yjq
     */
    @GetMapping("/getLabels")
    @ResponseBody
    public List<Label> getLabels(){
        return labelAdminService.selectAll();
    }

    /**
     * 修改标签
     * @param label
     * @return
     */
    @RequestMapping(value = "/updateLabel",method = RequestMethod.POST, consumes = "application/json;charset=utf-8")
    @ResponseBody
    public String updateLabel(@RequestBody Label label) {

        return labelAdminService.updateLabel(label);
    }

    /**
     * 添加标签
     * @param label
     * @return
     */
    @RequestMapping(value = "/addLabel",method = RequestMethod.POST, consumes = "application/json;charset=utf-8")
    @ResponseBody
    public String addLabel(@RequestBody Label label) {
        return labelAdminService.addLabel(label);
    }

    /**
     * 删除标签 重定向到editLabels
     * @param id
     * @return
     */
    @RequestMapping(value = "/deleteLabel",method = RequestMethod.GET)
    public String toDeleteLabel(Integer id) {
        labelAdminService.deleteLabelById(id);
        return "redirect:editLabels";
    }

}
